<template lang="html">
  <div class="menubar">
    <div class="menu-item" v-for="(item, index) in menulist" :class=" index == currentIndex ? 'active' : '' "
         @click="selectMenu(item,index)">
      <i class="iconfont" :class="item.icon"></i>{{item.text}}
    </div>
  </div>
</template>

<script>
  import router from 'router/index.js'
  import LocalDAO from 'common/localDAO'

  export default {
    data () {
      return {
        currentIndex: 0
      }
    },
    props: {
      menulist: {
        type: Array
      },
      routename: {
        type: String
      }
    },
    methods: {
      selectMenu (item,index) {
        this.$nextTick(function () {
          if (this.currentIndex !== index) {
            this.currentIndex = index
            var path = '';
            if (this.routename === 'bestmanbox') {
              path = '/greatman/'+ this.routename.toLowerCase() + '/' + index;
              this.sendManEvent(index);

            } else if (this.routename.toLowerCase() === 'beststockbox') {
              path = '/greatstock/'+ this.routename.toLowerCase() + '/' + index;

            } else if (this.routename.toLowerCase() === 'bestgroupbox') {
              path = '/greatstock/'+ this.routename + '/' + index;
            }
            this.$router.replace({path: path});
          }
        })
      },
      sendManEvent(index){
        var eventidArry = ["15002","15004","15006","15009"];
        var eventid = eventidArry[index];
        var userCode = LocalDAO.getUserCode();
        var kv = {"usercode": userCode,"k2":""};
        TDAPP.onEvent(eventid,"",kv);
        TDAPP.send();
      }
    },
    mounted(){
      switch(this.$route.path.toLowerCase()){
        case "/greatman/bestmanbox":
          this.$router.replace({path: "/greatman/bestmanbox/0"});
          this.sendManEvent(0);
          break;
        case "/greatman/bestmanbox/0":
          this.currentIndex = 0;
          break;
        case "/greatman/bestmanbox/1":
          this.currentIndex = 1;
          break;
        case "/greatman/bestmanbox/2":
          this.currentIndex = 2;
          break;
        case "/greatman/bestmanbox/3":
          this.currentIndex = 3;
          break;

        case "/greatstock/bestgroupbox":
          this.$router.replace({path: '/greatstock/bestgroupbox/0'});
          break;
        case "/greatstock/bestgroupbox/0":
          this.currentIndex = 0;
          break;
        case "/greatstock/bestgroupbox/1":
          this.currentIndex = 1;
          break;
        case "/greatstock/bestgroupbox/2":
          this.currentIndex = 2;
          break;
        case "/greatstock/bestgroupbox/3":
          this.currentIndex = 3;
          break;
      }
    }
  }
</script>

<style lang="css">
  .menubar {
    width: 100%;
    display: flex;
    justify-content: space-around;
    align-items: center;
    background-color: #CDE8F3;
    color: #FFFFFF;
    padding: 0 3%;
  }

  .menu-item {
    flex: 1;
    font-size: 12px;
    background-color: #7BC4E7;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    height: 30px;
    line-height: 23px;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .beststockbox .menu-item {
    margin: 10px 5px 0px 0px;
    width: 33.33%;
  }

  .bestgroupbox .menu-item, .bestmanbox .menu-item {
    margin: 10px 2px 0px 0px;
    width: 25%;
  }

  .menubar .active {
    background-color: #fff;
    color: #3FB9EE;
  }


  .beststockbox .menubar .iconfont {
    font-size: 25px !important;
  }

  .bestgroupbox .menubar .iconfont, .bestmanbox .menubar .iconfont {
    font-size: 22px !important;
  }
</style>
